//
//  YSReaderSetting.m
//  YSBookRead
//
//  Created by Kyson on 2018/11/6.
//  Copyright © 2018 YangShen. All rights reserved.
//

#import "YSReaderSetting.h"
#import "YSStorageManager.h"

#define YSTHEMERGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

NSArray *YSThemeImages() {
    static NSArray *themeImages;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        themeImages = @[ @"water_mode_bg", @"yellow_mode_bg", @"green_mode_bg", @"sheepskin_mode_bg", @"violet_mode_bg", @"pink_mode_bg", @"weekGreen_mode_bg", @"weekPink_mode_bg", @"coffee_mode_bg", @"blackGreen_mode_bg" ];
    });
    return themeImages;
}

@implementation YSReaderSetting {
    /// 是否需要更新文字属性字典
    BOOL _needUpdateAttributes;
    /// 文字属性字典
    NSDictionary *_readerAttributes;
}

+ (instancetype)shareReaderSettings {
    static YSReaderSetting *setting = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        setting = [YSStorageManager getSetting];
        if (!setting) {
            setting = [[YSReaderSetting alloc] init];
            [YSStorageManager updateSetting:setting];
        }
    });
    return setting;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _brightness = 0.7;
        _lineSpacing = 8;
        _fontSize = 16.0;
        _theme = YSReaderThemeOne;
        _pageStyle = YSTurnPageStylePageCurl;
        _isNightMode = NO;
    }
    return self;
}

#pragma mark - Getter & Setter
- (void)setTheme:(YSReaderTheme)theme {
    if (_theme == theme) { return; }

    _theme = theme;
    _needUpdateAttributes = YES;
    NSLog(@"主题更换");
    [YSStorageManager updateSetting:self];
    !self.updateSettingBlock ?: self.updateSettingBlock();
}

- (void)setFontSize:(CGFloat)fontSize {
    if (fontSize < kYSFontSizeMin || fontSize > kYSFontSizeMax) { return; }

    _fontSize = fontSize;
    _needUpdateAttributes = YES;
    NSLog(@"字体更换");
    [YSStorageManager updateSetting:self];
    !self.updateSettingBlock ?: self.updateSettingBlock();
}

- (void)setLineSpacing:(CGFloat)lineSpacing {
    if (lineSpacing < kYSLineSpacingMin || lineSpacing > kYSLineSpacingMax) { return; }

    _lineSpacing = lineSpacing;
    _needUpdateAttributes = YES;
    NSLog(@"行间距更换");
    [YSStorageManager updateSetting:self];
    !self.updateSettingBlock ?: self.updateSettingBlock();
}

- (void)setPageStyle:(YSTurnPageStyle)pageStyle {
    if (_pageStyle == pageStyle) { return; }
    _pageStyle = pageStyle;
    NSLog(@"翻页方式更换");
    [YSStorageManager updateSetting:self];
    !self.updateSettingBlock ?: self.updateSettingBlock();
}

- (void)setIsNightMode:(BOOL)isNightMode {
    if (_isNightMode == isNightMode) { return; }
    _isNightMode = isNightMode;
    [YSStorageManager updateSetting:self];
}

- (UIFont *)font {
    return [UIFont systemFontOfSize:self.fontSize];
}

- (UIColor *)textColor {
    switch (self.theme) {
        case YSReaderThemeNine:
            return YSTHEMERGB(149, 147, 143);
            break;
        case YSReaderThemeTen:
            return YSTHEMERGB(98, 112, 121);
            break;
        default:
            return [UIColor blackColor]; // YSTHEMERGB(33, 33, 33);
            break;
    }
}

- (UIColor *)otherTextColor {
    switch (self.theme) {
        case YSReaderThemeOne:
            return YSTHEMERGB(115, 111, 131);
            break;
        case YSReaderThemeTwo:
            return YSTHEMERGB(121, 104, 74);
            break;
        case YSReaderThemeThree:
            return YSTHEMERGB(129, 147, 129);
            break;
        case YSReaderThemeFour:
            return YSTHEMERGB(145, 132, 106);
            break;
        case YSReaderThemeFive:
            return YSTHEMERGB(145, 132, 106);
            break;
        case YSReaderThemeSix:
            return YSTHEMERGB(138, 133, 146);
            break;
        case YSReaderThemeSeven:
            return YSTHEMERGB(146, 139, 152);
            break;
        case YSReaderThemeEight:
            return YSTHEMERGB(128, 135, 107);
            break;
        case YSReaderThemeNine:
            return YSTHEMERGB(111, 108, 104);
            break;
        case YSReaderThemeTen:
            return YSTHEMERGB(59, 78, 87);
            break;
        default:
            return [UIColor blackColor];
            break;
    }
}

- (UIImage *)themeImage {
    return [UIImage imageNamed:YSThemeImages()[self.theme]];;
}

- (NSArray *)themeImageArr {
    return YSThemeImages();
}

- (NSDictionary *)readerAttributes {
    if (!_needUpdateAttributes && _readerAttributes) {
        return _readerAttributes;
    }

    _needUpdateAttributes = NO;

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];

    // 段落配置
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    // 行间距
    paragraphStyle.lineSpacing = self.lineSpacing;
    // 段间距
//    paragraphStyle.paragraphSpacing = 0;
    // 当前行间距(lineSpacing)的倍数(可根据字体大小变化修改倍数)
//    paragraphStyle.lineHeightMultiple = 1;
    // 对齐
    paragraphStyle.alignment = NSTextAlignmentJustified;

    [dic setValue:self.textColor forKey:NSForegroundColorAttributeName];
    [dic setValue:self.font forKey:NSFontAttributeName];
    [dic setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];

    return dic.copy;
}

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeDouble:self.lineSpacing forKey:@"lineSpacing"];
    [aCoder encodeDouble:self.fontSize forKey:@"fontSize"];
    [aCoder encodeInteger:self.theme forKey:@"theme"];
    [aCoder encodeInteger:self.pageStyle forKey:@"pageStyle"];
    [aCoder encodeBool:self.isNightMode forKey:@"isNightMode"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        _lineSpacing = [aDecoder decodeDoubleForKey:@"lineSpacing"];
        _fontSize    = [aDecoder decodeDoubleForKey:@"fontSize"];
        _theme       = [aDecoder decodeIntegerForKey:@"theme"];
        _pageStyle   = [aDecoder decodeIntegerForKey:@"pageStyle"];
        _isNightMode = [aDecoder decodeBoolForKey:@"isNightMode"];
    }
    return self;
}


@end
